package cn.walls1717.wallsmall.product.app;

import cn.walls1717.wallsmall.common.utils.PageUtils;
import cn.walls1717.wallsmall.common.utils.R;
import cn.walls1717.wallsmall.product.entity.ProductAttrValueEntity;
import cn.walls1717.wallsmall.product.service.AttrService;
import cn.walls1717.wallsmall.product.service.ProductAttrValueService;
import cn.walls1717.wallsmall.product.vo.AttrRespVO;
import cn.walls1717.wallsmall.product.vo.AttrVO;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 商品属性
 *
 * @author walls1717
 * @email walls1717@163.com
 * @date 2022-10-21 11:58:42
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Resource
    private AttrService attrService;
    @Resource
    private ProductAttrValueService productAttrValueService;

    /** 根据属性id查询属性信息 */
    @GetMapping("/info/inner/{attrId}")
    @Cacheable(value = "attr", key = "'attrinfo:' + #root.args[0]")
    public AttrRespVO attrInfo(@PathVariable("attrId") Long attrId) {
        return attrService.getAttrInfo(attrId);
    }

    /**
     * 获取spu规格
     */
    @GetMapping("/base/listforspu/{spuId}")
    public R listAttrBySpu(@PathVariable Long spuId) {
        List<ProductAttrValueEntity> productAttrValues = attrService.listProductAttrsBySpuId(spuId);
        return R.ok().put("data", productAttrValues);
    }

    /**
     * 修改商品规格
     */
    @PostMapping("/update/{spuId}")
    public R updateProductAttr(@RequestBody List<ProductAttrValueEntity> productAttrValues,
        @PathVariable("spuId") Long spuId) {
        productAttrValueService.updateProductAttrBySpuId(productAttrValues, spuId);
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/{type}/list/{catId}")
    // @RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("type") String type,
        @PathVariable("catId") Long catId) {
        PageUtils page = attrService.listPages(params, catId, type);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{attrId}")
    // @RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId) {
        AttrRespVO attr = attrService.getAttrInfo(attrId);
        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("product:attr:save");
    public R save(@RequestBody AttrVO attr) {
        attrService.saveAttrWithAttrAttrGroupRelation(attr);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVO attr) {
        attrService.updateAttrWithAttrAttrGroupRelation(attr);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));
        return R.ok();
    }

}
